import PageConst from './PageConst';


export default {

  defaults(props) {
    return {
      wHeight: '100vh',
      mHeight: 920 / 3,
      aHeight: 250,
      ...PageConst,
      // 项目工程数量统计信息
      eq: {
        ts: {
          a: '0',
          n: '0'
        },
        ztb: {
          a: '0',
          n: '0'
        },
        sght: {
          a: '0',
          n: '0'
        },
        xkz: {
          a: '0',
          n: '0'
        },
        wg: {
          a: '0',
          n: '0'
        }
      },
      // 图审基础数据
      ts: {
        clz: '0000',
        ywc: '0000'
      },

    }
  },

  // 项目工程数量统计信息
  async eq(ctx, props) {

    const content = await ctx.fn.DB.xmzx.info.eq({Authorization: ctx.getProps().token}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          eq: {$set: content}
        })
      );
    }
  },

  // 图审基础数据
  async ts(ctx, props) {
    const content = await ctx.fn.DB.xmzx.info.ts({Authorization: ctx.getProps().token}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          ts: {$set: content}
        })
      );
    }
  },

  // 招投标数据
  async ztb(ctx, props) {
    const content = await ctx.fn.DB.xmzx.info.ztb({Authorization: ctx.getProps().token}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      let ztb = {
        legend: [],
        series: {
          zj: [],
          jg: []
        }
      };

      content.category.map((kv) => {
        ztb.legend.push(kv.v);
      });
      content.zj.map((kv) => {
        ztb.series.zj.push({
          name: kv.k,
          value: kv.v
        });
      });
      content.jg.map((kv) => {
        ztb.series.jg.push({
          name: kv.k,
          value: kv.v
        });
      });

      ctx.setState({
        ztbOption: React.addons.update(ctx.getState().ztbOption,
          {
            legend: {
              data: {$set: ztb.legend}
            },
            series: {
              $apply: (series) => {
                return series.map(item=> {
                  if (item.name === '招投标在建工程数据') {
                    return React.addons.update(item, {data: {$set: ztb.series.zj}})
                  } else {
                    return React.addons.update(item, {data: {$set: ztb.series.jg}})
                  }
                })
              }
            }
          })
      });
    }
  },

  // 施工合同数据
  async sght(ctx, props) {

    const content = await ctx.fn.DB.xmzx.info.sght({Authorization: ctx.getProps().token}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      let sght = {
        xAxis: [],
        series: {
          js: [],
          wjs: [],
          sy: []
        }
      };
      content.category.map((kv) => {
        sght.xAxis.push(kv.v);
      });
      content.js.map((kv) => {
        sght.series.js.push(kv.v);
      });
      content.wjs.map((kv) => {
        sght.series.wjs.push(kv.v);
      });
      content.sy.map((kv) => {
        sght.series.sy.push(kv.v);
      });

      ctx.setState({
        sghtOption: React.addons.update(ctx.getState().sghtOption,
          {
            xAxis: {
              data: {$set: sght.xAxis}
            },
            series: {
              $apply: (series) => {
                return series.map(item=> {
                  if (item.name === '已决算') {
                    return React.addons.update(item, {data: {$set: sght.series.js}})
                  } else if (item.name === '未决算') {
                    return React.addons.update(item, {data: {$set: sght.series.wjs}})
                  } else {
                    return React.addons.update(item, {data: {$set: sght.series.sy}})
                  }
                })
              }
            }
          })
      });
    }
  },

  // 违规处理数据
  async wg(ctx, props) {

    const content = await ctx.fn.DB.xmzx.info.wg({Authorization: ctx.getProps().token}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      let wg = {
        xAxis: [],
        series: {
          zj: [],
          jg: []
        }
      };
      content.category.map((kv) => {
        wg.xAxis.push(kv.v);
      });
      content.zj.map((kv) => {
        wg.series.zj.push(kv.v);
      });
      content.jg.map((kv) => {
        wg.series.jg.push(kv.v);
      });

      ctx.setState({
        wgOption: React.addons.update(ctx.getState().wgOption,
          {
            xAxis: {
              data: {$set: wg.xAxis}
            },
            series: {
              $apply: (series) => {
                return series.map(item=> {
                  if (item.name === '全年') {
                    return React.addons.update(item, {data: {$set: wg.series.zj}})
                  } else {
                    return React.addons.update(item, {data: {$set: wg.series.jg}})
                  }
                })
              }
            }
          })
      });
    }
  },

  // 施工许可数据
  async xkz(ctx, props) {
    const content = await ctx.fn.DB.xmzx.info.xkz({Authorization: ctx.getProps().token}).catch(
      (error) => {
        console.log(error);
      }
    );
    if (content) {
      let xkz = {
        xAxis: [],
        series: {
          zj: [],
          jg: []
        }
      };
      content.category.map((kv) => {
        xkz.xAxis.push(kv.v);
      });
      content.zj.map((kv) => {
        xkz.series.zj.push(kv.v);
      });
      content.jg.map((kv) => {
        xkz.series.jg.push(kv.v);
      });

      ctx.setState({
        sgxkOption: React.addons.update(ctx.getState().sgxkOption,
          {
            xAxis: {
              data: {$set: xkz.xAxis}
            },
            series: {
              $apply: (series) => {
                return series.map(item=> {
                  if (item.name === '今年全部') {
                    return React.addons.update(item, {data: {$set: xkz.series.zj}})
                  } else {
                    return React.addons.update(item, {data: {$set: xkz.series.jg}})
                  }
                })
              }
            }
          })
      });
    }
  }
}
